package com.dr.controller;

import com.dr.domain.Result;
import com.dr.domain.User;
import com.dr.service.UserService;
import com.dr.utils.JwtUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * @Auther: yangjianfeng
 * @Date: 2025/07/04 14:18
 * @Description:
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    //登录功能
    @PostMapping("/login")
    @ApiOperation("完成登录功能")
    public Result login(@RequestBody User  user){
        String userName = user.getUserName();
        String password = user.getPassword();
        try { //alt+enter 纠错
            User dbUser=  userService.login(userName,password);
            //生成token返回
            HashMap<String, Object> map = new HashMap<>();
            map.put("name",dbUser.getUserName());
            map.put("id",Integer.toString(dbUser.getId()));
            //生成token令牌
            String token = JwtUtil.genToken(map);
            return  Result.success(token);
        }catch (Exception e){
            return Result.error("用户名或者密码错误");
        }
    }
}
